/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.view.global;

import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.common.utils.ApplicationException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by Peter on 2016/7/12.
 */
public class ImportFileDialog extends JDialog {

    /**
     * Constructor
     */
    public ImportFileDialog() {
        initComponents();
    }

    private void initComponents() {
        txtFile = new JTextField();
        btnChoice = new JButton();
        btnStart = new JButton();
        txtLog = new JTextArea();


        btnChoice.setText("File");
        btnChoice.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChoiceActionPerformed(evt);
            }
        });
        btnStart.setText("Start");
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        this.setLayout(new GridBagLayout());

        JPanel infoPanel = new JPanel();
        final Dimension infoPanelSize = new Dimension(400, 50);
        infoPanel.setPreferredSize(infoPanelSize);
        infoPanel.setBorder(BorderFactory.createTitledBorder(" Import "));
        infoPanel.setLayout(new GridBagLayout());
        infoPanel.add(txtFile, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0));
        infoPanel.add(btnChoice, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(10, 100));
        infoPanel.add(btnStart, new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(10, 100));

        JPanel logPanel = new JPanel();
        logPanel.setLayout(new GridBagLayout());
        final Dimension logPanelSize = new Dimension(400, 200);
        logPanel.setPreferredSize(logPanelSize);
        logPanel.setBorder(BorderFactory.createTitledBorder(" Log "));
        logPanel.add(txtLog, new GBC(0, 0).setFill(GBC.BOTH).setWeight(100, 100));

        this.add(infoPanel, new GBC(0, 0).setFill(GBC.BOTH).setWeight(100, 5).setInsets(5));
        this.add(logPanel, new GBC(0, 1).setFill(GBC.BOTH).setWeight(100, 100).setInsets(5));

    }

    private void btnChoiceActionPerformed(ActionEvent evt) {
        try {
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            jfc.showDialog(new JLabel(), "选择");
            File file = jfc.getSelectedFile();
            if (file != null) {
                txtFile.setText(file.getAbsolutePath());
            }
        } catch (ApplicationException ex) {

        }
    }

    private void btnStartActionPerformed(ActionEvent evt) {
    }

    public static void showDialog(Component relativeTo) {
        ImportFileDialog.showDialog(relativeTo, "File");
    }

    public static void showDialog(Component relativeTo, String title) {
        ImportFileDialog dialog = new ImportFileDialog();
        dialog.setTitle(title);
        dialog.setLocationRelativeTo(relativeTo);
        dialog.pack();
        dialog.setVisible(true);
    }


    private JTextField txtFile;
    private JButton btnChoice;
    private JButton btnStart;
    private JTextArea txtLog;
}
